package com.upay.android.controller.activity;

import java.lang.reflect.Field;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.ViewConfiguration;
import android.widget.Toast;

import com.upay.android.domain.response.ResponseEnvelope;
import com.upay.android.domain.response.ResponseError;
import com.upay.android.util.exception.RestException;
import com.upay.android.util.exception.RestExceptionType;


public abstract class UPayActivity extends Activity {
	
	/* ---- Constants ---- */

	/* ---- Instance Variables ---- */
	private ProgressDialog progressDialog;
	

	/* ---- Business Methods ---- */
	
	public void handleError(ResponseError responseError) {
		
	}
	
	public void processRestException(RestException exception) {
		if (RestExceptionType.ConnectionException.equals(exception.getRestExceptionType())) {
			//Intent intent = new Intent(getContext(), NoConnectionActivity.class);
			//startActivity(intent);
		}
		
        if (RestExceptionType.IllegalArgumentException.equals(exception.getRestExceptionType())) {
			
		}
        
        if (RestExceptionType.ParseException.equals(exception.getRestExceptionType())) {
	
        }
	}
	
	public <T extends Object> void processProgramException(ResponseEnvelope<T> response) {
		
	}
	
	public void showProgressDialog() {
		showProgressDialog("Loading. Please wait...");
	}
	
	public void showProgressDialog(String message) {
		if (progressDialog == null) {
			progressDialog = new ProgressDialog(getApplicationContext());
			progressDialog.setIndeterminate(true);
		}
		
		progressDialog.setMessage(message);
		progressDialog.show();
	}
	
	public void dismissProgressDialog() {
		if (progressDialog != null) {
			progressDialog.dismiss();
		}
	}
	
	public void showValidationErrors(String message, int gravity) {
		Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
		toast.setGravity(gravity, 0, 0);
		toast.show();
	}
	
	
	//***************************************
    // Protected methods
    //***************************************	
//	protected void processException(Exception e) {
//		if (e != null) {
//			if (e instanceof ResourceAccessException) {
//				displayNetworkError();
//			} else if (e instanceof HttpClientErrorException) {
//				HttpClientErrorException httpError = (HttpClientErrorException) e;
//				if (httpError.getStatusCode() ==  HttpStatus.UNAUTHORIZED) {
//					displayAuthorizationError();
//				}
//			}
//		}
//	}
//	
	protected void displayNetworkError() {
		Toast toast = Toast.makeText(this, "A problem occurred with the network connection while attempting to communicate with Greenhouse.", Toast.LENGTH_LONG);
		toast.setGravity(Gravity.CENTER, 0, 0);
		toast.show();
	}
	
	protected void displayAuthorizationError() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("You are not authorized to connect to Greenhouse. Please reauthorize the app.");
		builder.setCancelable(false);
		builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int id) {
		     	// TODO - signOut();
			}
		});
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	public void disableMenuButton() {
		try {
			ViewConfiguration config = ViewConfiguration.get(this);
			Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
			if(menuKeyField != null) {
			    menuKeyField.setAccessible(true);
			    menuKeyField.setBoolean(config, false);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

}
